package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.enums.HarmlessStatus;
import com.jiantong.work.impl.dao.ButcherAdmissionInspectionDao;
import com.jiantong.work.impl.dao.HarmlessDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.ButcherAdmissionInspection;
import com.jiantong.work.model.Harmless;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by niufei on 2017/3/20.
 * 生成无害化  尚未做
 */
@Component
public class ButcherAdmissionInspectionManager {

    private final ReportHandleResumeDao reportHandleResumeDao;

    private final ButcherAdmissionInspectionDao butcherAdmissionInspectionDao;

    private final HarmlessDao harmlessDao;

    @Autowired
    public ButcherAdmissionInspectionManager(ReportHandleResumeDao reportHandleResumeDao, ButcherAdmissionInspectionDao butcherAdmissionInspectionDao ,HarmlessDao harmlessDao) {
        this.reportHandleResumeDao = reportHandleResumeDao;
        this.butcherAdmissionInspectionDao = butcherAdmissionInspectionDao;
        this.harmlessDao = harmlessDao;
    }


    /**
     * 创建入场监督检查的单据
     * @param butcherAdmissionInspection 入场监督检查的单据
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> createButcherAdmissionInspection(ButcherAdmissionInspection butcherAdmissionInspection, Integer toStatus,LoginUser loginUser){
        Integer fromStatus = butcherAdmissionInspection.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();

        String prefix = "S";
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,butcherAdmissionInspectionDao.findCurrentMaxNumber(prefix));
        butcherAdmissionInspection.setNumber(number);
        butcherAdmissionInspection.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());

        if(toStatus!=null && !fromStatus.equals(toStatus)){
            butcherAdmissionInspection.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
            butcherAdmissionInspection.setStatus(toStatus);
            butcherAdmissionInspectionDao.create(butcherAdmissionInspection);
            map.put("butcherAdmissionInspectionId",butcherAdmissionInspection.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(butcherAdmissionInspection.getId());
            reportHandleResume.setReportNumber(butcherAdmissionInspection.getNumber());
            reportHandleResume.setType(DocumentsType.ADMISSION_INSPECTION.value());//入场监督检查
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());
        }else{
            butcherAdmissionInspectionDao.create(butcherAdmissionInspection);
            map.put("butcherAdmissionInspectionId",butcherAdmissionInspection.getId());
        }
        if(butcherAdmissionInspection.getUnqualifiedQuantity() > 0){
            //TODO 无害化处理
            Harmless harmless = buildHarmless(butcherAdmissionInspection,loginUser);
            harmlessDao.create(harmless);
            map.put("harmlessId",harmless.getId());
            butcherAdmissionInspection.setHarmlessId(harmless.getId());
            butcherAdmissionInspection.setHarmlessNumber(harmless.getNumber());
            butcherAdmissionInspectionDao.update(butcherAdmissionInspection);
        }
        return map;
    }

    /**
     * 更新入场监督检查的单据
     * @param butcherAdmissionInspection 入场监督检查的单据对象
     * @param toStatus 到状态
     * @return  Map<String , Object>
     */
    @Transactional
    public  Map<String , Object> updateButcherAdmissionInspection(ButcherAdmissionInspection butcherAdmissionInspection,Integer toStatus,LoginUser loginUser){
        Integer fromStatus = butcherAdmissionInspection.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();
        Boolean flagButcherAdmissionInspection ;

        if(toStatus!=null && !fromStatus.equals(toStatus)){
            butcherAdmissionInspection.setStatus(toStatus);
            flagButcherAdmissionInspection = butcherAdmissionInspectionDao.update(butcherAdmissionInspection);
            map.put("flagButcherAdmissionInspection",flagButcherAdmissionInspection);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(butcherAdmissionInspection.getId());
            reportHandleResume.setReportNumber(butcherAdmissionInspection.getNumber());
            reportHandleResume.setType(DocumentsType.ADMISSION_INSPECTION.value());//入场监督检查
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            flagButcherAdmissionInspection = butcherAdmissionInspectionDao.update(butcherAdmissionInspection);
            map.put("flagButcherAdmissionInspection",flagButcherAdmissionInspection);
        }

        if(butcherAdmissionInspection.getUnqualifiedQuantity() > 0){
            //TODO 无害化处理
            Harmless harmless = buildHarmless(butcherAdmissionInspection,loginUser);
            harmlessDao.create(harmless);
            map.put("harmlessId",harmless.getId());
            butcherAdmissionInspection.setHarmlessNumber(harmless.getNumber());
            butcherAdmissionInspection.setHarmlessId(harmless.getId());
            butcherAdmissionInspectionDao.update(butcherAdmissionInspection);
        }

        return map;
    }

    /**
     * 构建无害化单据
     * @param butcherAdmissionInspection 入场单据的对象
     * @return harmless 对象
     */
    private Harmless buildHarmless(ButcherAdmissionInspection butcherAdmissionInspection,LoginUser loginUser) {
        Harmless harmless = new Harmless();

        Integer isLocal = butcherAdmissionInspection.getIsLocal();
        String prefix = "WBD";
        if(!isLocal.equals(1)){
            prefix = "WWD";
        }
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,harmlessDao.findCurrentMaxNumber(prefix));

        harmless.setNumber(number);
        harmless.setReportId(butcherAdmissionInspection.getId());
        harmless.setReportNumber(butcherAdmissionInspection.getNumber());
        harmless.setReportType(DocumentsType.ADMISSION_INSPECTION.value());
        harmless.setButcherFactoryId(butcherAdmissionInspection.getButcherFactoryId());
        harmless.setButcherFactoryName(butcherAdmissionInspection.getButcherFactoryName());
        harmless.setAnimalType(butcherAdmissionInspection.getProductType());
        harmless.setUnit(butcherAdmissionInspection.getUnit());
        harmless.setQuantity(butcherAdmissionInspection.getUnqualifiedQuantity().floatValue());
        harmless.setQuarantineUserId(loginUser.getId());
        harmless.setQuarantineUserName(loginUser.getName());
        harmless.setFactoryOwnerName(butcherAdmissionInspection.getOwner());
        harmless.setStatus(HarmlessStatus.UNCOMMITTED.value());
        harmless.setIsLocal(butcherAdmissionInspection.getIsLocal());
        harmless.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        return harmless;
    }
}
